﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OpenSoftStore.BusinessLogic.Admin;
using OpenSoftStore.BusinessLogic.Member;
using OpenSoftStore.BusinessLogic.Money;
using OpenSoftStore.Client.UIL.ManagePrinter;
using OpenSoftStore.Orm.Model;
using OpenSoftStore.Orm.Static;
using OpenSoftStore.Orm.View;

namespace OpenSoftStore.Client.UIL.ManageMember
{
    public partial class DinnerSalePay : UserControl
    {
        public DinnerLogic LgcDinner;
        public List<T_Dinner> Tdinners;
        public T_Dinner Tdinner;
        public T_DinnerSale TdinnerSale;
        public DinnerSaleLogic LgcDinnerSale;
        public T_Member Tmember;
        public MemberLogic LgcMember;
        public List<V_Admin> Admins; 
        public List<T_Borakerage> Borakerages;
        public AdminLogic LgcAdmin;
        public Boolean Drog = false;
        public int Top = 0;
        public int Left = 0;

        public DinnerSalePay()
        {
            InitializeComponent();
            LgcDinner=new DinnerLogic();
            Tdinners=new List<T_Dinner>();
            Tdinner = new T_Dinner();
            TdinnerSale = new T_DinnerSale();
            LgcDinnerSale=new DinnerSaleLogic();
            Tmember = new T_Member();
            LgcMember=new MemberLogic();
            Borakerages=new List<T_Borakerage>();
            LgcAdmin=new AdminLogic();
            Borakerages = new List<T_Borakerage>();
            Admins=new List<V_Admin>();
            Tdinners = LgcDinner.GetDinners();
            cmbDinner.DataSource = Tdinners;
            cmbDinner.DisplayMember = "Name";
            cmbDinner.ValueMember = "Id";

            var moenyLogic = new MoneyLogic();
            var moenyList = moenyLogic.SqlWhere<T_Account>(" 1=1").ToList();
            var list = moenyList.Where(m => m.Name != "现金").ToList();
            cmbBankAccountId.DataSource = list;
            cmbBankAccountId.DisplayMember = "Name";
            cmbBankAccountId.ValueMember = "Id";

            Admins = LgcAdmin.GetAdminView().ToList();
            dgvCmbAdmin.DataSource = Admins;
            dgvCmbAdmin.DisplayMember = "Name";
            dgvCmbAdmin.ValueMember = "Id";
            if (Admins.Any())
            {
                dgvCmbAdmin.DefaultCellStyle.NullValue = Admins[0].Name;
                var borakerage = new T_Borakerage { AdminId = Admins[0].Id };
                Borakerages.Add(borakerage);
            }
            dgvBorakerage.DataSource = Borakerages;
        }

        private void DinnerSalePay_Load(object sender, EventArgs e)
        {
            cmbDinner.Focus();
        }

        private void pbClose_Click(object sender, EventArgs e)
        {
            var findForm = FindForm();
            if (findForm == null) return;
            findForm.DialogResult = DialogResult.Cancel;
            findForm.Close();
        }

        public void SetMember(string id)
        {
            Tmember.Id = id;
            TdinnerSale.MemberId = Tmember.Id;

            Tmember = LgcMember.SqlWhere<T_Member>(" Id='" + Tmember.Id + "'").FirstOrDefault();
            if (Tmember == null) return;
            lblHaveValue.Text = Tmember.HaveValue + "元";
          
        }

        private void cmbDinner_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtNeedPay.Text = ((T_Dinner)cmbDinner.SelectedItem).Price.ToString();
            nudCashPay.Value = ((T_Dinner) cmbDinner.SelectedItem).Price;
            nudTimes.Value = ((T_Dinner)cmbDinner.SelectedItem).Times;
            txtOwedMoney.Text = "0";
            TdinnerSale.DinnerId = ((T_Dinner) cmbDinner.SelectedItem).Id;

            if (Tmember.HaveValue >= ((T_Dinner)cmbDinner.SelectedItem).Price && Tmember.HaveValue > 0)
            {
                nudValuePay.Value = ((T_Dinner)cmbDinner.SelectedItem).Price;
                nudCashPay.Value = 0;
                txtPass.ReadOnly = false;
                txtPass.Focus();
            }
            else if (Tmember.HaveValue < ((T_Dinner)cmbDinner.SelectedItem).Price && Tmember.HaveValue > 0)
            {
                nudValuePay.Value = Tmember.HaveValue;
                nudCashPay.Value = ((T_Dinner)cmbDinner.SelectedItem).Price - Tmember.HaveValue;
                txtPass.ReadOnly = false;
                txtPass.Focus();
            }
            SetTxtOwedMoney();
        }

        public void SetTxtOwedMoney()
        {
            TdinnerSale.NeedPay = Convert.ToDecimal(txtNeedPay.Text);
            TdinnerSale.CashPay = nudCashPay.Value;
            TdinnerSale.PayMark = nudValuePay.Value;
            TdinnerSale.BankPay = nudBankPay.Value;
            TdinnerSale.FavorMoney = nudFavorMoney.Value;
            TdinnerSale.ValuePay = nudValuePay.Value;

            TdinnerSale.OwedMoney = TdinnerSale.NeedPay
                              - TdinnerSale.ValuePay
                              - TdinnerSale.CashPay
                              - TdinnerSale.BankPay
                              - TdinnerSale.FavorMoney;
            txtOwedMoney.Text = TdinnerSale.OwedMoney.ToString();
        }

        private void BtnSave_Click(object sender, EventArgs e)
        {
            Tmember.Pass = txtPass.Text;
            if (TdinnerSale.ValuePay > 0)
            {
                if (!LgcMember.ExitMemberByPass(Tmember))
                {
                    FormSysMessage.ShowSuccessMsg("充值卡密码错误！");
                    txtPass.Focus();
                    txtPass.Text = "";
                    return;
                }
            }
            if (TdinnerSale.OwedMoney < 0)
            {
                FormSysMessage.ShowSuccessMsg("欠款金额不能为负数！");
                return;
            }
            TdinnerSale.AdminId = S_Admin.Id;
            TdinnerSale.ShopId = S_Admin.ShopId;
            TdinnerSale.BankAccountId = cmbBankAccountId.SelectedValue.ToString();
            TdinnerSale.Times = Convert.ToInt32(nudTimes.Value);
            dgvBorakerage.EndEdit(DataGridViewDataErrorContexts.Commit);
            int row = LgcDinnerSale.Save(TdinnerSale, Borakerages);
            string message = "";
            message = row == 0 ? "保存失败" : "保存成功";
            if (row == 0) FormSysMessage.ShowSuccessMsg(message);
            else MemberList.Instance.btnSearch_Click(null,null);


            DinnerPrintWindow.Instance.SetReport(TdinnerSale.Id);
            //print.SetReport("OpenSoftStore.Client.UIL.ManagePrinter.BillPrint.rdlc", "DataSet1", data);
            DinnerPrintWindow.Instance.ShowDialog();

            var findForm = FindForm();
            if (findForm == null) return;
            //findForm.DialogResult = DialogResult.Cancel;
            findForm.DialogResult = DialogResult.OK;
            findForm.Close();

        }

        private void nudCashPay_KeyUp(object sender, KeyEventArgs e)
        {
            SetTxtOwedMoney();
        }

        private void nudValuePay_KeyUp(object sender, KeyEventArgs e)
        {
            SetTxtOwedMoney();
            txtPass.ReadOnly = nudValuePay.Value <= 0;
        }

        private void nudFavorMoney_KeyUp(object sender, KeyEventArgs e)
        {
            SetTxtOwedMoney();
        }

        private void nudBankPay_KeyUp(object sender, KeyEventArgs e)
        {
            SetTxtOwedMoney();
        }

        private void BtnCancel_Click(object sender, EventArgs e)
        {
            var findForm = FindForm();
            if (findForm == null) return;
            findForm.DialogResult = DialogResult.Cancel;
            findForm.Close();
        }

        private void btnBorakerageAdd_Click(object sender, EventArgs e)
        {
            dgvBorakerage.DataSource = null;
            //dgvBorakerage.EndEdit(DataGridViewDataErrorContexts.Commit);
            var borakerage = new T_Borakerage { AdminId = Admins[0].Id };
            Borakerages.Add(borakerage);
            dgvBorakerage.DataSource = Borakerages;
            dgvBorakerage.Focus();
        }

        private void btnBorakerageDelete_Click(object sender, EventArgs e)
        {
            if (dgvBorakerage.CurrentRow == null) return;
            var index = dgvBorakerage.CurrentRow.Index;
            Borakerages.RemoveAt(index);
            dgvBorakerage.DataSource = null;
            dgvBorakerage.DataSource = Borakerages;
            if (index > 0) dgvBorakerage.CurrentCell = dgvBorakerage.Rows[index - 1].Cells[0];
            dgvBorakerage.Focus();
        }

        private void PnlInfoTitle_MouseDown(object sender, MouseEventArgs e)
        {
            Drog = true;
            Top = e.Y;
            Left = e.X;
        }

        private void PnlInfoTitle_MouseMove(object sender, MouseEventArgs e)
        {
            if (!Drog) return;
            var findForm = FindForm();
            if (findForm == null) return;
            findForm.Top += e.Y - Top;
            findForm.Left += e.X - Left;
        }

        private void PnlInfoTitle_MouseUp(object sender, MouseEventArgs e)
        {
            Drog = false;
        }

    }
}
